const cloud = require('cloud1-2g029zan6f8e16b1');

cloud.init({ env: cloud.DYNAMIC_CURRENT_ENV }); // 使用动态环境ID
const db = cloud.database();

exports.main = async (event, context) => {
  const { code } = event;
  if (!code) {
    throw new Error('缺少登录凭证');
  }

  try {
    // 调用微信API获取openid和unionid
    const res = await cloud.cloud.callFunction({
      name: 'login',
      data: { code }
    });
    return res.result;
  } catch (error) {
    console.error('获取openid失败:', error);
    throw new Error('登录失败，请重试');
  }
};